package com.egene.tw.activity.member;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.egene.tw.R;
import com.egene.tw.base.RecycleBaseAdapter;
import com.egene.tw.model.HealthListItem;
import com.egene.tw.utils.ImageUtil;

/**
 * Created by WZX on 2015/8/16.
 */
public class HealthRecycleAdapter extends RecycleBaseAdapter {
    public static final int TITLE = 1;
    public static final int CONTENT = 0;
    public static final int CONTENT_1 = 2;

    public HealthRecycleAdapter() {
    }

    public HealthRecycleAdapter(View headerView) {
        mHeaderView = headerView;
    }

    @Override
    public int getItemViewType(int position) {
        HealthListItem item = (HealthListItem) _data.get(position);
        return item.getViewType();
    }

    @Override
    public View onCreateItemView(ViewGroup parent, int viewType) {
        if(viewType == TITLE) {
            return getLayoutInflater(parent.getContext()).inflate(R.layout.item_title, null);
        }else if(viewType == CONTENT_1){
            return getLayoutInflater(parent.getContext()).inflate(R.layout.item_left_record, null);
        }else {
            return getLayoutInflater(parent.getContext()).inflate(R.layout.item_health, null);
        }
    }

    @Override
    protected RecycleBaseAdapter.ViewHolder onCreateItemViewHolder(View view, int viewType) {
        if(viewType == TITLE) {
            return new ViewHolderTitle(viewType, view);
        }else if(viewType == CONTENT_1){
            return new ViewHolderTitle(viewType, view);
        }else {
            return new ViewHolder(viewType, view);

        }
    }

    @Override
    protected void onBindItemViewHolder(RecycleBaseAdapter.ViewHolder vh, int position) {
        super.onBindItemViewHolder(vh, position);
        if(getItemViewType(position) == TITLE){
            HealthRecycleAdapter.ViewHolderTitle holder = (HealthRecycleAdapter.ViewHolderTitle) vh;
            HealthListItem item = (HealthListItem) _data.get(position);
            holder.tvTitle.setText(item.getTitle());
        }else if(getItemViewType(position) == CONTENT_1){
            HealthRecycleAdapter.ViewHolderTitle holder = (HealthRecycleAdapter.ViewHolderTitle) vh;
            HealthListItem item = (HealthListItem) _data.get(position);
            holder.tvTitle.setText(item.getTitle());
        }else {
            HealthRecycleAdapter.ViewHolder holder = (HealthRecycleAdapter.ViewHolder) vh;
            HealthListItem item = (HealthListItem) _data.get(position);
            ImageUtil.loadImage(holder.ivHead.getContext(), holder.ivHead, item.getThumb_img_url());
            holder.ivHead.setVisibility(item.isNoImage()? View.GONE:View.VISIBLE);
//            holder.tvContent.setVisibility(item.isNoImage()? View.GONE:View.VISIBLE);
            holder.tvTitle.setText(item.getTitle());
            holder.tvContent.setText(item.getZhaiyao());
        }

    }

    public static class ViewHolder extends RecycleBaseAdapter.ViewHolder {
        ImageView ivHead;
        TextView tvTitle;
        TextView tvContent;

        public ViewHolder(int viewType, View view) {
            super(viewType, view);
            ivHead = (ImageView) view.findViewById(R.id.ivHead);
            tvTitle = (TextView) view.findViewById(R.id.tvTitle);
            tvContent = (TextView) view.findViewById(R.id.tvContent);
        }
    }

    public static class ViewHolderTitle extends RecycleBaseAdapter.ViewHolder {
        TextView tvTitle;

        public ViewHolderTitle(int viewType, View view) {
            super(viewType, view);
            tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        }
    }
}
